#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <assert.h>
#include <math.h>

#include "adv7441.h"
#include "adv7441_def.h"

void printhelp(void)
{
	printf("1--write a register\n" );
	printf("2--read a register\n" );	
}


int main(int argc, char **argv)
{
	int dev;
	//unsigned int chip;
	unsigned int addr;
	unsigned int reg;
	unsigned int value;
	adv7400_w_reg advreg;
	int para ;
	
	if (argc != 2) {
    	printf("usage :%s 1|2\n",argv[0]);
		printhelp();
		return 0;
	}

	dev = open("/dev/adv7441", O_RDONLY);
	if (dev <= 0) {
		printf("adv7441 device open fail\n");
		exit(0);
	}

	para = atoi(argv[1]);
	switch(para)
	{
		case 0:	
			/* 
			 * read all register value . for example 
			 */
			printf("please input I2C chip:\n");

			break ;

		case 1:	/* write */
			printf("(write...)please input I2C addr, reg and reg-value:\n") ;
			scanf("%x %x %x",&addr,&reg,&value);			
			//printf("chip = 0x%x, reg = 0x%x, value = 0x%x\n", chip, reg, value);

			advreg.addr = (unsigned char)addr;
			advreg.reg = (unsigned char)reg;
			advreg.value = (unsigned char)value;			
			ioctl(dev, ADV7441_WRITE_REG, &advreg);
			printf("addr = 0x%x, reg = 0x%x, value = 0x%x\n", advreg.addr, advreg.reg, advreg.value);
			break ;
		
		case 2:	/* read */
			printf("(read...)please input I2C addr, reg:\n") ;
			scanf("%x %x",&addr,&reg);	
			//printf("chip = 0x%x, reg = 0x%x\n", chip, reg);
			
			advreg.addr = (unsigned char)addr;
			advreg.reg = (unsigned char)reg;
			advreg.value = 0;	
			ioctl(dev, ADV7441_READ_REG, &advreg);
			printf("chip = 0x%x, reg = 0x%x, value = 0x%x\n", advreg.addr, advreg.reg, advreg.value);			
			break ;
		
		default :
			break ;
	}

	close(dev);

	return 0;
}
